package com.user.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.user.entity.User;
import com.user.service.Page;
import com.user.service.SortHelper;
import com.user.service.UserQryHelper;
import com.user.service.UserService;

@Controller
@RequestMapping("/user")
public class UserAction{
	
	@Autowired
	private UserService userService;
	
	private SortHelper sh = new SortHelper("userId");
	
	/**
	 * 创建用户输入
	 * @return
	 */
	@RequestMapping("/input.do")
	public String input()
	{
		return "user/input_user";
	}
	
	/**
	 * 创建用户
	 * @return
	 */
	@RequestMapping("/add.do")
	public String create(User user)
	{
			userService.createUser(user);
		return "redirect:list.do";
	}
	/**
	 * 进入主页面
	 * @return
	 */
	@RequestMapping("/login.do")
	public String login()
	{
		return "main";
	}
	/**
	 * 根据id获取用户
	 * @return  修改视图
	 */
	@RequestMapping("/edit.do")
	public String preModify(User user, Model model)
	{
		user = userService.getUserById(user.getUserId());
		model.addAttribute("user", user);
		return "user/edit_user";
	}
	/**
	 * 更新用户信息
	 * @return 用户列表
	 */
	@RequestMapping("/modify.do")
	public String modify(User user)
	{
		userService.modifyUser(user);
		return "redirect:list.do";
	}
	/**
	 * 注销用户
	 * @return 用户列表
	 */
	@RequestMapping("/delete.do")
	public String remove(User user)
	{
		userService.removeUser(user);
		return "redirect:list.do";
	}
	/**
	 * 用户列表
	 * @return
	 */
	@RequestMapping("/list.do")
	public String list(Model model, UserQryHelper helper, Page page)
	{
		helper.setSortHelper(sh);
		page = userService.loadAll(page, helper);
		model.addAttribute("page", page);
		model.addAttribute("helper", helper);
		return "user/list_user";
	}


}
